<?php
namespace app\api\model;

use think\Model;

class Thirddb extends Model
{

    protected $pk = 'third_id';

    protected $name = 'third';

    public $param = array();

    public function scopeOrderId($query, $order_id)
    {
        $query->where('order_id', $order_id);
    }

    public function scopeRoomId($query, $room_id)
    {
        $query->where('room_id', $room_id);
    }

    public function scopeThirdId($query, $third_id)
    {
        $query->where('third_id', $third_id);
    }

    public function scopeState($query)
    {
        $query->where('state', 0);
    }

    public function scopeAddress($query, $address)
    {
        $query->where('address', $address);
    }
    // 基础查询
    public function base($query)
    {
        if (isset($this->param['order_id']) && $this->param['order_id'] != '') {
            $query->orderId($this->param['order_id']);
        }
        if (isset($this->param['room_id']) && $this->param['room_id'] != '') {
            $query->roomId($this->param['room_id']);
        }
        if (isset($this->param['third_id']) && $this->param['third_id']) {
            $query->thirdId($this->param['third_id']);
        }
        if (isset($this->param['state']) && $this->param['state'] != '') {
            $query->state();
        }
        if (isset($this->param['address']) && $this->param['address']) {
            $query->address($this->param['address']);
        }
    }

    public function ajaxFind()
    {
        $data = $this->find();
        if ($data) {
            $data = $data->toArray();
        } else {
            $data = array();
        }
        return $data;
    }

    public function ajaxSave()
    {
        if (isset($this->param['third_id'])) {
            $this->save($this->param, [
                'third_id' => $this->param['third_id']
            ]);
        } else {
            $this->allowField(true)->save($this->param);
        }
        return 'success';
    }
}